package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.EmployeeQueryObject;
import cn.wolfcode.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

// 班级管理控制器
@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {
    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IEmployeeService employeeService;

    //查询    /classinfo/list.do
    @RequestMapping("/list")
    public String list(Model model , @ModelAttribute("qo")EmployeeQueryObject qo){
        // 查询所有班主任共享给页面
        model.addAttribute("employees",employeeService.listAll());
        // 共享高查分页数据给页面
        PageInfo<Classinfo> pageResult = classinfoService.query(qo);
        model.addAttribute("pageResult",pageResult);
        return "classinfo/list";
    }

    //删除    /classinfo/delete.do?id=?
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        classinfoService.deleteById(id);
        return new JsonResult();
    }

    //修改或者新增
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo){
        if (classinfo.getId() != null) {
            classinfoService.update(classinfo);
        }else {
            classinfoService.insert(classinfo);
        }
        return new JsonResult();
    }

}
